<template>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
    @close="closeDialog()"
    :visible.sync="visible"
    width="700px">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="180px">
      <el-form-item label="数据源" prop="type">
        <el-select v-model="dataForm.type" placeholder="请选择数据源类型" @change="changeSelect">
          <el-option
            v-for="dict in dbTypeValue"
            :key="dict.label"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="数据源名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="数据源名称"></el-input>
      </el-form-item>
      <el-form-item label="描述" prop="note">
        <el-input v-model="dataForm.note" placeholder="描述"></el-input>
      </el-form-item>
      <el-form-item label="IP主机名" prop="host">
        <el-input v-model="dataForm.host" placeholder="IP主机名"></el-input>
      </el-form-item>
      <el-form-item label="端口" prop="port">
        <el-input v-model="dataForm.port" placeholder="端口"></el-input>
      </el-form-item>
      <el-form-item label="用户名" prop="userName">
        <el-input v-model="dataForm.userName" placeholder="用户名"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="dataForm.password" placeholder="密码"></el-input>
      </el-form-item>
      <el-form-item label="数据库名" prop="database">
        <el-input v-model="dataForm.database" placeholder="数据库名"></el-input>
      </el-form-item>
      <el-form-item label="defaultFs" prop="defaultFs" v-if="dataForm.type=='HIVE'">
        <el-input v-model="dataForm.defaultFs" placeholder="Hadoop hdfs文件系统namenode节点地址"></el-input>
      </el-form-item>
      <el-form-item label="服务名或SID" prop="connectType" v-if="dataForm.type=='ORACLE'">
        <el-radio v-model="dyRadio" label="ORACLE_SERVICE_NAME" >服务名</el-radio>
        <el-radio v-model="dyRadio" label="ORACLE_SID">SID</el-radio>
      </el-form-item>
      <el-form-item label="jdbc链接参数" prop="connectionParams">
        <el-input v-model="dataForm.connectionParams" placeholder="数据源链接参数"></el-input>
      </el-form-item>
      <el-form-item label="启用状态" prop="status">
        <el-switch
          v-model="switchVal"
          active-color="#409eff"
          inactive-color="#dcdfe6">
        </el-switch>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false" >取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {getObj, addObj, putObj} from '@/api/integration/itgdatasource'
import {remote} from '@/api/admin/dict'

export default {
  data() {
    return {
      title:'',
      visible: false,
      canSubmit: false,
      dbTypeValue:[],
      dataForm: {
        database: '',
        host: '',
        id: '',
        name: '',
        note: '',
        other: null,
        type: 'MYSQL',
        password: '',
        port:'3306',
        status: '',
        userName: '',
        connectionParams: '',
        connectType: '',
        defaultFs:'',
      },
      switchVal: true,
      dyRadio: 'ORACLE_SERVICE_NAME',
      dataRule: {
        name: [
          {required: true, message: '数据源名称不能为空', trigger: 'blur'}
        ],
        dbType: [
          {required: true, message: '数据源类型不能为空', trigger: 'blur'}
        ],
        database: [
          {required: true, message: '数据库名不能为空', trigger: 'blur'}
        ],
        host: [
          {required: true, message: 'IP主机名不能为空', trigger: 'blur'}
        ],
        port: [
          {required: true, message: '端口不能为空', trigger: 'blur'}
        ],
        userName: [
          {required: true, message: '用户名不能为空', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '密码不能为空', trigger: 'blur'}
        ],
        status: [
          {required: true, message: '状态不能为空', trigger: 'blur'}
        ],
        defaultFs: [
          {required: true, message: 'defaultFs不能为空', trigger: 'blur'}
        ],
      }
    }
  },
  created() {
    remote('datasource_type').then(response => {
      this.dbTypeValue = response.data.data;
    });
  },
  methods: {
    init(id) {
      this.visible = true;
      this.canSubmit = true;
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if(this.switchVal==true){
          this.dataForm.status = 0
        }else{
          this.dataForm.status = 1
        }
        if(this.dyRadio=='ORACLE_SERVICE_NAME'){
          this.dataForm.connectType = "ORACLE_SERVICE_NAME"
        }else{
          this.dataForm.connectType = "ORACLE_SID"
        }
        if (id) {
          this.title = "修改数据源";
          getObj(id).then(response => {
            this.dataForm = response.data.data
            if(this.dataForm.status==0){
              this.switchVal = true
            }else{
              this.switchVal = false
            }
          })
        }else{
          this.title = "创建数据源";
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.canSubmit = false;
          if (this.dataForm.id) {
            // console.log(this.dataForm)
            putObj(this.dataForm).then(data => {
              this.$notify.success('修改成功')
              this.visible = false
              this.$emit('refreshDataList')
            }).catch(() => {
              this.canSubmit = true;
            });
          } else {
            // console.log(this.dataForm)
            addObj(this.dataForm).then(data => {
              this.$notify.success('添加成功')
              this.visible = false
              this.$emit('refreshDataList')
            }).catch(() => {
              this.canSubmit = true;
            });
          }
        }
      })
    },
    //重置表单
    closeDialog() {
      this.$refs["dataForm"].resetFields()
    },
    //select切换
    changeSelect(val){
      console.log(val)
      let defaultPort = ''
      switch(val){
        case 'MYSQL':
          defaultPort = '3306'
          break
        case 'ORACLE':
          defaultPort = '1521'
          break
        case 'SQLSERVER':
          defaultPort = '1433'
          break
        case 'POSTGRESQL':
          defaultPort = '5432'
          break
        case 'DB2':
          defaultPort = '50000'
          break
        case 'HIVE':
          defaultPort = '10000'
          break
        case 'CLICKHOUSE':
          defaultPort = '8123'
          break
        default:
          break
      }
      this.dataForm.port = defaultPort
      return defaultPort
    },


  }
}
</script>
